// pages/library/library.js
Page({
    data: {
        searchText: '',
        searchHist: [],
        showHist: false,
        downSymbol: "▼",
        upSymbol: "▲",
      },
    
      // 监听搜索框输入
      inputChange: function(e) {
        this.setData({
          searchText: e.detail.value
        });
      },
    
      // 点击搜索按钮
      onSearch: function() {
        let hist = this.data.searchHist;
        let text = this.data.searchText;
        if(text) {
          hist.push(text);
          if(hist.length > 5){
            hist=hist.slice(hist.length -5 );//最近搜索的5条记录
          }
         
          this.setData({
            searchHist: hist
          });
          wx.setStorageSync('searchHist', hist);
        }
      },
    
      // 点击取消按钮
      onCancel: function() {
        this.setData({
          searchText: ''
        });
      },
    
      // 点击“展开”/“收起”图标
      onShowHist: function() {
        this.setData({
          showHist: !this.data.showHist
        });
      },
    
      // 点击“清空搜索历史”
      onClear: function() {
        this.setData({
          searchHist: []
        });
        wx.removeStorageSync('searchHist');
      },
      onSearchHistItem: function(e) {
        const searchText = e.currentTarget.dataset.item;
        // 调用搜索逻辑进行搜索，将 searchText 作为参数传递
        this.onSearch(searchText);
      },
    
      onLoad: function(options) {
        let hist = wx.getStorageSync('searchHist');
        if(hist) {
            if(hist.length > 5){
                hist = hist.slice(hist.length - 5);
            }
           
          this.setData({
            searchHist: hist
          });
        }
      },
     

    
    //   clickShow(e) {
    //     const index = e.currentTarget.dataset.index;
    //     this.setData({
    //       currentIndex: index
    //     });
    //     wx.navigateTo({
    //       url: '/pages/library/popularCategories/popularCategories'
    //     });
    //   },
    clickShow: function (event) {
        const index = event.currentTarget.dataset.index;
        let name = ''; // 根据index获取对应的标签名称
        if (index === "1") {
          name = '爱国';
        } else if (index === "2") {
          name = '友情';
        } else if (index === "3") {
         name ='爱情';
        }else if (index === "4") {
            name ='思乡';
           }else if (index === "5") {
            name ='田园';
           }else if (index === "6") {
            name ='抒怀';
           }else if (index === "7") {
            name ='怀古';
           }else if (index === "8") {
            name ='赠别';
           }else if (index === "9") {
            name ='民生';
           }else if (index === "10") {
            name ='边塞';
           }else if (index === "11") {
            name ='离别';
           }else if (index === "12") {
            name ='思念';
           }else if (index === "13") {
            name ='感恩';
           }else if (index === "14") {
            name ='伤感';
           }else if (index === "15") {
            name ='悼亡';
           }else if (index === "16") {
            name ='孤独';
           }
        wx.navigateTo({
          url: '/pages/library/popularCategories/popularCategories?name=' + name
        })
      },
// 热门应用

     popularApp:function(e){
const index = e.currentTarget.dataset.index;
let name='';
if(index === "23"){
   name ='写雨';
}else if(index ==="24"){
    name = '写雪';
}else if(index ==="25"){
    name = '写风';
}else if(index ==="26"){
    name = '写花';
}else if(index ==="27"){
    name = '写山';
}else if(index ==="28"){
    name = '写水';
}else if(index ==="29"){
    name = '春天';
}else if(index ==="30"){
    name = '夏天';
}else if(index ==="31"){
    name = '秋天';
}else if(index ==="32"){
    name = '冬天';
}else if(index ==="33"){
    name = '母亲';
}else if(index ==="34"){
    name = '老师';
}
wx.navigateTo({
  url: '/pages/library/popularApplication/popularApplication?name=' + name,
})
      },
    //   热门搜索
      search_clickShow:function(e){
        const index = e.currentTarget.dataset.index;
        let name = ''; // 根据index获取对应的标签名称
        if (index === "17") {
            name = '幼儿唐诗大全';
          } else if (index === "18") {
            name = '小学必背古诗';
          } else if (index === "19") {
           name ='初中必背古诗';
          }else if (index === "20") {
              name ='初中必背文言文';
             }else if (index === "21") {
              name ='高中必背古诗';
             }else if (index === "22") {
              name ='高中必背文言文';
             }
        wx.navigateTo({
          url: '/pages/library/popularSearch/popularSearch?name='+name
        })
      },
     

    
    



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})